import pygame,random,time
import os
# pygame.font.SysFont('arial',32)

# 当前目录下的文件
fp="./pong.game2.txt"
# 判断文件是否存在  
if not os.path.exists(fp):  
    with open(fp, 'w') as file:  
        # 写入字符串到文件中  
        file.write('1050889\n')  
        file.write('1112791\n')  
        file.write('1109856\n')  
        file.write('1107551\n')  
        file.write('1000\n')  
        file.write('1809374959\n')  
else:  
    print('文件已经存在')


pygame.init()
yunxing=True
def playponggame3(pygame,random,time,yunxing):
    def shua(pygame):
        pygame.display.flip()
    w=[600,800]
    x=[0,30]
    y,c=[0,20],[255,0,0]
    x2=50
    def hui(fp,fen):
        f=open(fp,"w+")
        s=str(fen[0])
        s1=str(fen[1])
        s2=str(fen[2])
        s3=str(fen[3])
        s4=str(fen[4])
        s5=str(int(time.time()))
        f.writelines([s+"\n",s1+"\n",s2+"\n",s3+"\n",s4+"\n",s5+"\n"])
    f=open(fp,"r+")
    oldfen=int(f.readline(100))
    fen1=int(f.readline(100))
    fen2=int(f.readline(100))
    fen3=int(f.readline(100))
    fencha=int(f.readline(100))
    timet=int(f.readline(100))
    min=0
    def tulifen(fen):
        tt=time.time()
        tt-=fen[6]
        tt=int(tt/100)
        #print(tt)
        oldfen=fen[0]
        fen1=fen[1]+tt
        fen2=fen[2]+tt
        fen3=fen[3]+tt
        fencha=fen[4]
        xx=True
        while xx:
            if oldfen<fen3:
                fen[5]=int((fen3-oldfen)/fencha)
                xx=False
            if oldfen>fen3:
                if oldfen>fen2:
                    if oldfen>fen1:
                        fen[5]=1
                        xx=False
                    else:
                        fen[5]=2
                        xx=False
                else:
                    fen[5]=3
                    xx=False
        min=fen[5]
        tt=time.time()
        fen=[oldfen,fen1,fen2,fen3,fencha,min,tt]
        return fen
    fen=[oldfen,fen1,fen2,fen3,fencha,min,timet]
    fen=tulifen(fen)
    #坐标，坐标，位移，位移，是否出界，剩余球数，分数
    ball=[random.randint(100,500),random.randint(100,500),4,4,False,3,0,0,0,0]
    screen=pygame.display.set_mode(w)
    screen.fill((255,255,255))
    shua(pygame)
    def ballzhuang(ball,x):
        if ball[0]<=10:
            ball[2]=-(ball[2])
        if ball[0]>=590:
            ball[2]=-(ball[2])
        if ball[1]<=10:
            ball[3]=-(ball[3])
        if ball[1]>770:
            #70
            if ball[0]<x-100 or ball[0]>x+100:
                ball[4]=True
                ball[5]-=1
                ball[8]+=1
            elif ball[7]==0:
                ball[3]=-(ball[3])
                #*ball[5]#碰撞加分
                ball[6]+=(-(ball[3])/2)*ball[5]
                ball[9]+=1
            ball[7]=7
        if ball[7]>0:
            ball[7]-=1
        ball[0]+=ball[2]
        ball[1]+=ball[3]
        return ball
    def drawflip(screen,c,x,y):
        pygame.draw.rect(screen,c,[int(x[0]),int(y[0]),int(x[1]-x[0]),int(y[1]-y[0])],0)       
    def draw(screen,x,y,x1,y1,c):
        drawflip(screen,c,x,y)
        if x1>x[0] and x1<x[1]:
            if y1>y[0] and y1<y[1]:
                return True
            else:
                return False
        else:
            return False
    def pygameprint(screen,p,pygame,daxiao,c,weizhi):
        score=str(p)
        font = pygame.font.Font(None, daxiao)
        my_score = font.render(str(score), False, c)
        screen.blit(my_score, (weizhi[0], weizhi[1])) 
        shua(pygame)
    def drawlines(pygame,screen):
        pygame.draw.lines(screen,[255,255,255],True,[[int(x[0]),int(y[0])],[int(x[1]),int(y[1])]],2)
        pygame.draw.lines(screen,[255,255,255],True,[[int(x[1]),int(y[0])],[int(x[0]),int(y[1])]],2)
    drawflip(screen,c,x,y)
    drawlines(pygame,screen)
    shua(pygame)
    def drawfen(pygame,screen,fen):
        pygame.draw.rect(screen,[155,155,155],[150,600,300,150],0)
        pygame.draw.rect(screen,[55,55,55],[150,600,300,30],1)
        pygame.draw.rect(screen,[55,55,55],[150,630,300,30],1) 
        pygame.draw.rect(screen,[55,55,55],[150,660,300,30],1) 
        pygame.draw.rect(screen,[55,55,55],[150,690,300,30],1)
        pygame.draw.rect(screen,[55,55,55],[150,720,300,30],1)
        x=0
        while x<4:
            font = pygame.font.Font(None, 50)
            if x<3:
                screen.blit(font.render(str(x+1), False, [0,0,0]), (150,600+x*30))
            if x==3:
                screen.blit(font.render(str(fen[5]), False, [0,0,0]), (150,630+x*30))
            x+=1
        p=[fen[0],fen[1],fen[2],fen[3]]
        p.sort()
        #print(p)
        my_score = font.render(str(p[3]), False, [0,0,0])
        screen.blit(my_score, (275,600+0*30))
        my_score = font.render(str(p[2]), False, [0,0,0])
        screen.blit(my_score, (275,630+0*30))
        my_score = font.render(str(p[1]), False, [0,0,0])
        screen.blit(my_score, (275,660+0*30))
        my_score = font.render(str(fen[0]), False, [0,0,0])
        screen.blit(my_score, (275,720+0*30))
        shua(pygame)
    def w1(screen,pygame,w,fen,yunxing):
        z1=[200,400,500,550]
        c=[255,0,0]
        drawflip(screen,c,[z1[0],z1[1]],[z1[2],z1[3]])
        pygameprint(screen,"play",pygame,60,[255,255,255],[z1[0]+50,z1[2]])
        drawfen(pygame,screen,fen)
        shua(pygame)
        r=True
        while r:
            for event in pygame.event.get():
                if  event.type == pygame.MOUSEBUTTONUP:
                    if event.button==1:
                        x1,y1=event.pos
                        if draw(screen,x,y,x1,y1,c):
                            r=False
                            yunxing=False
                        elif draw(screen,[z1[0],z1[1]],[z1[2],z1[3]],x1,y1,c):
                            r=False
                        else:
                            drawflip(screen,c,[z1[0],z1[1]],[z1[2],z1[3]])
                            drawflip(screen,c,x,y)
                            drawlines(pygame,screen)
                            pygameprint(screen,"play",pygame,60,[255,255,255],[z1[0]+50,z1[2]])
            shua(pygame)
        pygame.draw.rect(screen,[255,255,255],[0,0,int(w[0]),int(w[1])],0) 
        drawflip(screen,c,x,y)
        drawlines(pygame,screen)
        shua(pygame)
        if yunxing==False:
            pygame.quit()
        return yunxing

    yunxing=w1(screen,pygame,w,fen,yunxing)
    while yunxing:
        clock=pygame.time.Clock()
        r=True
        while r:
            for event in pygame.event.get():
                if  event.type == pygame.MOUSEBUTTONUP:
                    if event.button==1:
                        x1,y1=event.pos
                        if draw(screen,x,y,x1,y1,c):
                            fen[0]+=int(ball[6])#/
                            fen=tulifen(fen)
                            r=False
                            yunxing=w1(screen,pygame,[600,800],fen,yunxing)
            if yunxing:                
                screen.fill((255,255,255))
                x2,y2 = pygame.mouse.get_pos()
                drawflip(screen,c,[0,30],[0,20])
                drawlines(pygame,screen)
                ball=ballzhuang(ball,x2)
                #x2-70,140
                pygame.draw.rect(screen,[0,255,0],[x2-100,780,200,20],0)
                pygame.draw.circle(screen, [random.randint(0,255),random.randint(0,255),random.randint(0,255)], [ball[0],ball[1]], 10)
                font = pygame.font.Font(None, 60)
                my_score = font.render("score:", True, c)
                screen.blit(my_score, (350, 0))
                #
                score=int(ball[6])
                my_score = font.render(str(score), True, c)
                screen.blit(my_score, (500, 0))
                weizhi=ball[5]
                ww=350
                while weizhi>0:
                    pygame.draw.circle(screen,[255,0,0],[ww+10,50],10)
                    ww+=30
                    weizhi-=1
                while ball[4]:
                    if ball[5]>0:
                        drawflip(screen,c,[200,400],[500,550])
                        drawflip(screen,c,x,y)
                        drawlines(pygame,screen)
                        pygameprint(screen,"play",pygame,60,[255,255,255],[250,500])
                        pygame.display.update()
                        for event in pygame.event.get():
                            if  event.type == pygame.MOUSEBUTTONUP:
                                if event.button==1:
                                    x1,y1=event.pos
                                    if draw(screen,[200,400],[500,550],x1,y1,c):
                                        ball[4]=False
                                        ball[1]=random.randint(100,500)
                                        ball[0]=random.randint(100,500)
                                        pygame.draw.circle(screen, [255,0,0], [ball[0],ball[1]], 10)
                                        pygame.display.update()
                                        time.sleep(0.5)
                                    if draw(screen,x,y,x1,y1,c):
                                        yunxing=w1(screen,pygame,w,fen,yunxing)
                                        ball[1]=100
                                        ball[0]=100
                                        ball[4]=False
                                        r=False
                    if ball[5]<=0:
                        screen.fill((255,255,255))
                        drawflip(screen,c,x,y)
                        drawlines(pygame,screen)
                        my_score = font.render("game.over", True, c)
                        screen.blit(my_score, (200, 450))
                        pygame.display.update()
                        time.sleep(2)
                        fen[0]+=int(ball[6])
                        fen=tulifen(fen)
                        yunxing=w1(screen,pygame,[600,800],fen,yunxing)
                        if yunxing==False:
                            fen[0]+=int(ball[6])
                            hui(fp,fen)
                            f.close()
                            break
                        ball=[random.randint(100,500),random.randint(100,500),4,4,False,3,0,0,0,0]
                    drawflip(screen,c,[200,400],[500,550])
                    drawflip(screen,c,x,y)
                if ball[9]>=5:
                    ball[5]+=1
                    ball[9]-=5
                    if ball[3]<10 and ball[3]>-10:#10
                        if ball[3]>0:
                            ball[3]+=2
                        if ball[3]<0:
                            ball[3]-=2
                        if ball[2]>0:
                            ball[2]+=2
                        if ball[2]<0:
                            ball[2]-=2
            if yunxing:
                pygame.display.update() 
                clock.tick(50)#30
    fen[0]+=int(ball[6])
    hui(fp,fen)
    f.close()

playponggame3(pygame,random,time,yunxing)

pygame.quit()